//
//  ViewController.m
//  09-UITableViewEdit
//
//  Created by 402030900@qq.com on 16/7/26.
//  Copyright © 2016年 402030900@qq.com. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong)NSMutableArray<NSIndexPath *>*selectIndexArray;// 泛型 选中下标的数组
@property(nonatomic,strong)NSMutableArray * nameArray;
@property(nonatomic,strong)NSMutableArray<NSString *>*selectNameArray;//选中名字的数组
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview: _tableView];
    self.selectIndexArray = [NSMutableArray array];
    self.selectNameArray = [NSMutableArray array];
    NSArray * array = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    _nameArray = [NSMutableArray arrayWithArray:array];
    
    // tableView编辑
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;// 右按钮为编辑按钮
    NSLog(@"%@",NSStringFromSelector(self.editButtonItem.action));
//    self.editButtonItem.action = @selector(editAction);
    
    
    
    
    
    
    
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
}
//-(void)editAction {
//    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
//    if (self.editing == NO) {
//        // 修改数据源
//       [_nameArray removeObjectAtIndex:indexPath.row];
//// 修改对应cell
//       [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
//       [_selectNameArray removeAllObjects];
//       [_selectIndexArray removeAllObjects];
//
//
//    }
//}
//让tableView进入编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
   [super setEditing:editing animated:animated];// 重写方法要super
    [_tableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    }else{
        // 要删除选中的数组
        // 删除数据源
        [_nameArray removeObjectsInArray:_selectNameArray];
        // 删除cell
        [_tableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:(UITableViewRowAnimationLeft)];
        [_selectNameArray removeAllObjects];
        [_selectIndexArray removeAllObjects];
        NSLog(@"完成");
    }
}
// 控制tableView是否可以编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
#pragma mark 只有这个是delegate
// 返回cell的编辑状态(样式)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 1|2; // 可以多选
//    UITableViewCellEditingStyleNone,
//    UITableViewCellEditingStyleDelete,// 默认
//    UITableViewCellEditingStyleInsert
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.editing) {
        // 取出对应下标的名字
        NSString * name = _nameArray[indexPath.row];
          // 添加到选中名字的数组中
        [_selectNameArray addObject:_nameArray[indexPath.row]];
          // 在全选状态下,数组中添加对应选中的下标
        [_selectIndexArray addObject:indexPath];
    }
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.editing) {
       NSString * name =  _nameArray[indexPath.row];
        [_selectNameArray removeObject:name];
        // 在全选状态下,数组中移除对应选中的下标
        [_selectIndexArray removeObject:indexPath];
    }
}
// 对应 delete 或 insert 按钮点击方法(滑动删除必要)
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
#warning  如果对tableView进行添加/删除 先修改数据源再去对应进行添加/删除
        // 修改数据源
        [_nameArray removeObjectAtIndex:indexPath.row];
        // 修改对应cell
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
        NSLog(@"删除");
    }else if (editingStyle ==UITableViewCellEditingStyleInsert){
        [_nameArray insertObject:@"小王子" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
        NSLog(@"插入");
    }
    // tableView 不论删除 或者添加 都已经进行刷新操作

}
// 移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES; // 默认yes 不写方法也有效果
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    // 更改数据源
    [_nameArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    NSLog(@"%@",_nameArray);
}
#pragma mark --tableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _nameArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = _nameArray[indexPath.row];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
